package com.salesys.tony.service;

import com.salesys.gr.dao.ConverDao;
import com.salesys.gr.dao.RecordDao;
import com.salesys.gr.dao.UserDao;
import com.salesys.gr.util.LoginedUserHelper;
import com.salesys.gr.util.MD5Helper;
import com.salesys.gr.util.TimeHelper;
import com.salesys.tony.domain.Reset;
import com.salesys.tony.domain.User;
import com.salesys.tony.domain.Withdraw;

import java.util.ArrayList;
import java.util.List;

public class UserService {
	// 用户登录并取得用户基本信息
	public User login(User loginUser) {
		User loginingUser = new User();
		loginUser.setPassword(new MD5Helper().getTwiceMD5ofString(loginUser.getPassword()));
		loginingUser = new UserDao().login(loginUser);
		return loginingUser;
	}

	// 注册用户
	public int regUser(User regUser) {
		int affectRow = 0;
		int affectRow1 = 0;
		int affectRow2 = 0;
		int affectRow3 = 0;
		int newUserParent = 0;
		regUser.setPassword(new MD5Helper().getTwiceMD5ofString(regUser.getPassword()));
		do {
			affectRow1 = new UserDao().regUser(regUser);
		} while (affectRow1 != 1);
		String child = new UserDao().queryChildFromUsername(LoginedUserHelper.getLoginedUserUsername());
		int newChildId = new UserDao().queryIdFromUsernameAndPwd(regUser);
		if (new UserDao().queryInfoFromUsername(LoginedUserHelper.getLoginedUserUsername()).getMemberId() != null) {
			newUserParent = LoginedUserHelper.getLoginedUserId();
			do {
				affectRow2 = new UserDao().setChild(LoginedUserHelper.getLoginedUserUsername(), child, newChildId + "");
			} while (affectRow2 != 1);
		} else {
			String parent = null;
			String parentUsername = LoginedUserHelper.getLoginedUserUsername();
			String parentMemberId = null;
			do {
				parent = new UserDao().queryInfoFromUsername(parentUsername).getParent();
				parentUsername = new UserDao().queryUsernameFromId(parent).getUsername();
				parentMemberId = new UserDao().queryInfoFromUsername(parentUsername).getMemberId();
			} while (parentMemberId == null);
			child = new UserDao().queryChildFromUsername(parentUsername);
			newUserParent = Integer.parseInt(parent);
			do {
				affectRow2 = new UserDao().setChild(parentUsername, child, newChildId + "");
			} while (affectRow2 != 1);
		}
		do {
			affectRow3 = new UserDao().modifyParent(newChildId + "", newUserParent + "");
		} while (affectRow3 != 1);
		if (affectRow1 != 0 && affectRow2 != 0 && affectRow3 != 0) {
			affectRow = 1;
		}
		return affectRow;
	}

	// 从用户名查询上级
	public String queryParentUsername(String parentId) {
		return new UserDao().queryUsernameFromId(parentId).getUsername();
	}

	// 从下级ID查询下级用户名
	public List<String> queryChildUsernameFromChild(String child) {
		List<String> childList = null;
		if (child == null) {
			return childList;
		}
		childList = new ArrayList<String>();
		String Child[] = child.split(",");
		for (String x : Child) {
			childList.add(new UserDao().queryUsernameFromId(x).getUsername());
		}
		return childList;
	}

	// 获得下级数量
	public int getChildNum(String child) {
		int childNum = 0;
		if (child == null) {
			return childNum;
		} else {
			String Child[] = child.split(",");
			childNum = Child.length;
			return childNum;
		}
	}

	// 从用户名查询下级
	public String queryChildFromUsername(String Username) {
		String child = null;
		child = new UserDao().queryChildFromUsername(Username);
		return child;
	}

	// 从用户名查询上级
	public String queryParentFromUsername(String userName) {
		String parent = (new UserDao().queryInfoFromUsername(userName)).getParent();
		return parent;
	}

	// 从用户名查询资料
	public User queryInfoFromUsername(String Username) {
		User infoUser = new User();
		infoUser = new UserDao().queryInfoFromUsername(Username);
		return infoUser;
	}

	// 从会员卡号查询资料
	public User queryInfoFromMemberId(String memberId) {
		User infoUser = new User();
		infoUser = new UserDao().queryInfoFromMemberId(memberId);
		return infoUser;
	}

	// 验证旧密码
	public int verifyOldPwd(String Password, User modifyPwdUser) {
		int affect = 0;
		if (new MD5Helper().getTwiceMD5ofString(Password).equals(modifyPwdUser.getPassword())) {
			affect = 1;
		}
		return affect;
	}

	// 验证新密码
	public int verifyNewPwd(String newPwd_1, String newPwd_2) {
		int affect = 0;
		if (newPwd_1.equals(newPwd_2)) {
			affect = 1;
		}
		return affect;
	}

	// 修改用户资料
	public int modifyInfo(User modifyInfoUser) {
		int affectRow = 0;
		do {
			affectRow = new UserDao().modifyInfo(modifyInfoUser);
		} while (affectRow != 1);
		return affectRow;
	}

	// 修改会员卡号
	public int modifyMemberId(String memberId, String username) {
		int affectRow = 0;
		affectRow = new UserDao().modifyMemberId(memberId, username);
		return affectRow;
	}

	// 修改密码
	public int modifyPwd(User modifyPwdUser) {
		int affectRow = 0;
		modifyPwdUser.setPassword(new MD5Helper().getTwiceMD5ofString(modifyPwdUser.getPassword()));
		do {
			affectRow = new UserDao().modifyPwd(modifyPwdUser);
		} while (affectRow != 1);
		return affectRow;
	}

	// 申请重置密码
	public int resetPSW(String username, String email) {
		int affectRow = 0;
		affectRow = new UserDao().resetPSW(username, email);
		return affectRow;
	}

	// 处理重置密码申请
	public List<Reset> queryPSWReset() {
		List<Reset> resetList = new ArrayList<Reset>();
		resetList = new UserDao().queryPSWReset();
		return resetList;
	}

	// 重置密码
	public int modifyPwd(String username) {
		int affectRow = 0;
		String password = new MD5Helper().getTwiceMD5ofString("123456");
		do {
			affectRow = new UserDao().modifyPwd(username, password);
		} while (affectRow != 1);
		return affectRow;
	}

	// 删除重置密码申请
	public int deletePSWReset(String username) {
		int affectRow = 0;
		do {
			affectRow = new UserDao().deletePSWReset(username);
		} while (affectRow != 1);
		return affectRow;
	}

	// 申请提现
	public int withdrawRequest(String username, String money) {
		int affectRow = 0;
		affectRow = new UserDao().withdrawRequest(username, money);
		return affectRow;
	}

	// 处理提现申请
	public List<Withdraw> queryWithdraw() {
		List<Withdraw> withdrawList = new ArrayList<Withdraw>();
		withdrawList = new UserDao().queryWithdraw();
		return withdrawList;
	}

	// 提现
	public int withdraw(String username, String mark) {
		int affectRow1 = 0;
		int affectRow2 = 0;
		int affect = 0;
		String oldMark = new UserDao().queryInfoFromUsername(username).getMark();
		int newMark = Integer.parseInt(oldMark) - Integer.parseInt(mark);
		int userId = queryInfoFromUsername(username).getId();
		do {
			affectRow1 = new UserDao().withdraw(username, newMark + "");
			affectRow2 = new RecordDao().addRecord(userId, "-" + mark, "withdraw", TimeHelper.getSystemTime());
		} while (affectRow1 != 1 || affectRow2 != 1);
		if (affectRow1 == 1 && affectRow2 == 1) {
			affect = 1;
		}
		return affect;
	}

	// 删除提现申请
	public int deleteWithdraw(String username) {
		int affectRow = 0;
		do {
			affectRow = new UserDao().deleteWithdraw(username);
		} while (affectRow != 1);
		return affectRow;
	}

	// // 为当前用户增加积分
	// public int addMarkForSelf() {
	// int affectRow1 = 0;
	// int affectRow2 = 0;
	// int affect = 0;
	// int oldMark = 0;
	// if (((new
	// UserDao().queryInfoFromUsername(LoginedUserHelper.getLoginedUserUsername())).getMark())
	// != null) {
	// oldMark = Integer.parseInt((new
	// UserDao().queryInfoFromUsername(LoginedUserHelper.getLoginedUserUsername())).getMark());
	// } else {
	// oldMark = 0;
	// }
	// String markPercent = new
	// ConverDao().queryMarkPercentFromMarkSource("recommend");
	// if (markPercent != null) {
	// String newMark = ((int) (200 * Integer.parseInt(markPercent) * 0.01) +
	// oldMark) + "";
	// do {
	// affectRow1 = new
	// RecordService().addMarkRecord(LoginedUserHelper.getLoginedUserId(),
	// "recommend");
	// } while (affectRow1 != 1);
	// do {
	// affectRow2 = new UserDao().addMark(newMark,
	// LoginedUserHelper.getLoginedUserUsername());
	// } while (affectRow2 != 1);
	// }
	// if (affectRow1 == 1 && affectRow2 == 1) {
	// affect = 1;
	// }
	// return affect;
	// }

	// 修改余额
	public int changeMark(String username, String oldMark, String newMark) {
		int affect1 = 0;
		int affect2 = 0;
		int affect = 0;
		if (Integer.parseInt(oldMark) < Integer.parseInt(newMark)) {
			do {
				affect1 = new UserDao().addMark(newMark, username);
			} while (affect1 != 1);
			int userId = new UserDao().queryInfoFromUsername(username).getId();
			String mark = (Integer.parseInt(newMark) - Integer.parseInt(oldMark)) + "";
			do {
				affect2 = new RecordDao().addRecord(userId, mark, "recommend", TimeHelper.getSystemTime());
			} while (affect2 != 1);
		} else if (Integer.parseInt(oldMark) > Integer.parseInt(newMark)) {
			do {
				affect1 = new UserDao().addMark(newMark, username);
			} while (affect1 != 1);
			int userId = new UserDao().queryInfoFromUsername(username).getId();
			String mark = (Integer.parseInt(oldMark) - Integer.parseInt(newMark)) + "";
			do {
				affect2 = new RecordDao().addRecord(userId, "-" + mark, "withdraw", TimeHelper.getSystemTime());
			} while (affect2 != 1);
		}
		if (affect1 == 1 && affect2 == 1) {
			affect = 1;
		}
		return affect;
	}

	// 为上级增加现金
	public int changeMarkForParent(String username, String oldMark, String newMark) {
		int affectRow1 = 0;
		int affectRow2 = 0;
		int affect = 0;
		int parentId = -1;
		int chlidId = new UserDao().queryInfoFromUsername(username).getId();
		int childNumber = 1;
		int oldParentMark = 0;
		int changedMark = 0;
		if (Integer.parseInt(oldMark) < Integer.parseInt(newMark)) {
			changedMark = Integer.parseInt(newMark) - Integer.parseInt(oldMark);
		} else if (Integer.parseInt(oldMark) > Integer.parseInt(newMark)) {
			changedMark = Integer.parseInt(oldMark) - Integer.parseInt(newMark);
		}
		String parent = null;
		String newParentMark = null;
		parent = (new UserDao().queryInfoFromUsername(username).getParent());
		if (parent == null) {
			affect = 1;
			return affect;
		}
		while (parentId != 0) {
			parent = null;
			affectRow1 = 0;
			affectRow2 = 0;
			parent = new UserDao().queryInfoFromUsername(username).getParent();
			if (parent == null) {
				parentId = 0;
			} else {
				parentId = Integer.parseInt(parent);
				username = (new UserDao().queryUsernameFromId(parent)).getUsername();
				if (((new UserDao().queryInfoFromUsername(username)).getMark()) != null) {
					oldParentMark = Integer.parseInt((new UserDao().queryInfoFromUsername(username)).getMark());
				} else {
					oldParentMark = 0;
				}
				String markPercent = new ConverDao().queryMarkPercentFromMarkSource("child_" + childNumber);
				if (markPercent != null) {
					newParentMark = ((int) (changedMark * Integer.parseInt(markPercent) * 0.01) + oldParentMark) + "";
					do {
						affectRow1 = new RecordService().addParentMarkRecord(parentId, (int) (changedMark * Integer.parseInt(markPercent) * 0.01) + "", "child_" + childNumber + "_" + chlidId);
					} while (affectRow1 != 1);
					do {
						affectRow2 = new UserDao().addMark(newParentMark, username);
					} while (affectRow2 != 1);
					chlidId = parentId;
					if (affectRow1 == 1 && affectRow2 == 1) {
						affect = 1;
					}
				}
			}
			childNumber++;
		}
		return affect;
	}

	// 自动增加现金
	public int autoChangeMarkForParent(String username) {
		int affectRow1 = 0;
		int affectRow2 = 0;
		int affect = 0;
		int parentId = -1;
		int chlidId = new UserDao().queryInfoFromUsername(username).getId();
		int childNumber = 0;
		int oldParentMark = 0;
		int changedMark = 200;
		String markPercent = null;

		String parent = null;
		String newParentMark = null;
		parent = (new UserDao().queryInfoFromUsername(username).getParent());
		if (parent == null) {
			affect = 1;
			return affect;
		}
		while (parentId != 0) {
			parent = null;
			affectRow1 = 0;
			affectRow2 = 0;
			parent = new UserDao().queryInfoFromUsername(username).getParent();
			if (parent == null) {
				parentId = 0;
			} else {
				parentId = Integer.parseInt(parent);
				username = (new UserDao().queryUsernameFromId(parent)).getUsername();
				if (((new UserDao().queryInfoFromUsername(username)).getMark()) != null) {
					oldParentMark = Integer.parseInt((new UserDao().queryInfoFromUsername(username)).getMark());
				} else {
					oldParentMark = 0;
				}
				if (childNumber == 0) {
					markPercent = new ConverDao().queryMarkPercentFromMarkSource("recommend");
					if (markPercent != null) {
						newParentMark = ((int) (changedMark * Integer.parseInt(markPercent) * 0.01) + oldParentMark) + "";
						do {
							affectRow1 = new RecordService().addParentMarkRecord(parentId, (int) (changedMark * Integer.parseInt(markPercent) * 0.01) + "", "recommend");
						} while (affectRow1 != 1);
						do {
							affectRow2 = new UserDao().addMark(newParentMark, username);
						} while (affectRow2 != 1);
						chlidId = parentId;
						if (affectRow1 == 1 && affectRow2 == 1) {
							affect = 1;
						}
					}
				} else {
					markPercent = new ConverDao().queryMarkPercentFromMarkSource("child_" + childNumber);
					if (markPercent != null) {
						newParentMark = ((int) (changedMark * Integer.parseInt(markPercent) * 0.01) + oldParentMark) + "";
						do {
							affectRow1 = new RecordService().addParentMarkRecord(parentId, (int) (changedMark * Integer.parseInt(markPercent) * 0.01) + "", "child_" + childNumber + "_" + chlidId);
						} while (affectRow1 != 1);
						do {
							affectRow2 = new UserDao().addMark(newParentMark, username);
						} while (affectRow2 != 1);
						chlidId = parentId;
						if (affectRow1 == 1 && affectRow2 == 1) {
							affect = 1;
						}
					}
				}
			}
			childNumber++;
		}
		return affect;
	}

	// // 为上级增加积分
	// public int addMarkForParent(String userName) {
	// int affectRow1 = 0;
	// int affectRow2 = 0;
	// int affect = 0;
	// int parentId = -1;
	// int chlidId = LoginedUserHelper.getLoginedUserId();
	// int childNumber = 2;
	// int oldMark = 0;
	// String parent = null;
	// String newMark = null;
	// parent = (new UserDao().queryInfoFromUsername(userName).getParent());
	// if (parent == null) {
	// affect = 1;
	// return affect;
	// }
	// while (parentId != 0) {
	// parent = null;
	// affectRow1 = 0;
	// affectRow2 = 0;
	// parent = new UserDao().queryInfoFromUsername(userName).getParent();
	// if (parent == null) {
	// parentId = 0;
	// } else {
	// parentId = Integer.parseInt(parent);
	// userName = (new UserDao().queryUsernameFromId(parent)).getUsername();
	// if (((new UserDao().queryInfoFromUsername(userName)).getMark()) != null)
	// {
	// oldMark = Integer.parseInt((new
	// UserDao().queryInfoFromUsername(userName)).getMark());
	// } else {
	// oldMark = 0;
	// }
	// String markPercent = new
	// ConverDao().queryMarkPercentFromMarkSource("child_" + childNumber);
	// if (markPercent != null) {
	// newMark = ((int) (200 * Integer.parseInt(markPercent) * 0.01) + oldMark)
	// + "";
	// do {
	// affectRow1 = new RecordService().addParentMarkRecord(parentId, (int) (200
	// * Integer.parseInt(markPercent) * 0.01) + "", "child_" + childNumber +
	// "_" + chlidId);
	// } while (affectRow1 != 1);
	// do {
	// affectRow2 = new UserDao().addMark(newMark, userName);
	// } while (affectRow2 != 1);
	// chlidId = parentId;
	// if (affectRow1 == 1 && affectRow2 == 1) {
	// affect = 1;
	// }
	// }
	// }
	// childNumber++;
	// }
	// return affect;
	// }

	// 删除用户
	public int deleteUser(String id, String username, String parent) {
		int affect = 0;
		int affect1 = 0;
		int affect2 = 0;
		int affect3 = 0;
		String parentUsername = new UserDao().queryUsernameFromId(parent).getUsername();
		String child = new UserDao().queryInfoFromUsername(parentUsername).getChild();
		String childChild = new UserDao().queryInfoFromUsername(username).getChild();
		String newChild = "";
		String Child[] = child.split(",");
		for (String x : Child) {
			if (x.equals(id)) {
			} else {
				newChild = newChild + x + ",";
			}
		}
		if (childChild != null) {
			String ChildChild[] = childChild.split(",");
			for (String x : ChildChild) {
				newChild = newChild + x + ",";
				do {
					affect3 = 0;
					affect3 = new UserDao().modifyParent(x, parent);
				} while (affect3 != 1);
			}
		} else {
			affect3 = 1;
		}
		do {
			affect2 = new UserDao().modifyChild(parentUsername, newChild);
		} while (affect2 != 1);
		do {
			affect1 = new UserDao().deleteUser(id);
		} while (affect1 != 1);
		if (affect1 == 1 && affect2 == 1 && affect3 == 1) {
			affect = 1;
		}
		return affect;
	}

	// 查询登录标记
	public int isLogined(String username) {
		int isLogined = 0;
		isLogined = new UserDao().isLogined(username);
		return isLogined;
	}

	// 修改登录标记
	public int isLogined(String isLogined, String username) {
		int affect = 0;
		do {
			affect = new UserDao().isLogined(isLogined, username);
		} while (affect != 1);
		return affect;
	}

	// 检查更新
	public String checkUpdate() {
		String version = new UserDao().checkUpdate();
		return version;
	}
}
